﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.HR;
using CE.Job;
using CE.User;

public partial class HR_job_application_details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.HRId] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!IsPostBack)
        {
            if (Request.Params["aid"] != null)
            {
                int _aid = int.Parse(Request.Params["aid"].ToString());
                Application _application = new Application();
                ApplicationDB _applicationdb = new ApplicationDB();
                _application = _applicationdb.GetApplicationByID(_aid);

                Account _account = new Account();
                AccountDB _accountdb = new AccountDB();
                _account = _accountdb.GetAccount(_application.RefUserID);

                Profile _profile = new Profile();
                ProfileDB _profiledb = new ProfileDB();
                _profile = _profiledb.GetProfile(_application.RefUserID);

                lblName.Text = _profile.Salutation + " " + _profile.Name;
                lblContactNumber.Text = _profile.ContactNumber;
                lblDOB.Text = _profile.DOB.ToString("dd MMM yyyy");
                lblEmailAddress.Text = _account.Email;
                lblQualification.Text = _profile.Qualification;
                lblYearsExperience.Text = _profile.YearsExperience;
                lblSalesExperience.Text = _profile.SalesExperience;
                lblEmpStatus.Text = _profile.EmploymentStatus;
                lblOccupation.Text = _profile.Occupation;
                lblCountry.Text = _profile.PreferredCountry;
                lblSummary.Text = _application.Comments;
                ddlApplicationStatus.SelectedValue = _application.ApplicationStatus;
                txtRemark.Text = _application.Remark;
                lblCreatedDate.Text = _application.CreatedDate.ToString() ;

                if (_profile.AttachCV != "")
                {
                    if (System.IO.File.Exists(_profile.AttachCV))
                    {
                        hidCV.Value = _profile.AttachCV;
                    }
                    else
                    {
                        btnDownloadCV.Text = "File not exists";
                        btnDownloadCV.Enabled = false;
                    }
                }
                else
                {
                    btnDownloadCV.Text = "No CV upload";
                    btnDownloadCV.Enabled = false;
                }

                if (_profile.VoiceRecord != "")
                {
                    if (System.IO.File.Exists(_profile.VoiceRecord))
                    {
                        hidVR.Value = _profile.VoiceRecord;
                    }
                    else
                    {
                        btnDownloadVR.Text = "File not exists";
                        btnDownloadVR.Enabled = false;
                    }
                }
                else
                {
                    btnDownloadVR.Text = "No Record";
                    btnDownloadVR.Enabled = false;
                }

                Job _job = new Job();
                JobDB _jobdb = new JobDB();
                _job = _jobdb.GetJobByID((int)_application.RefJobID);
                lblJob.Text = "<a href='../careers/job-details.aspx?jid=" + _job.JobID + "' target='_blank'>" + _job.JobTitle + "</a>";
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int _aid = int.Parse(Request.Params["aid"].ToString());
        Application _application = new Application();
        ApplicationDB _applicationdb = new ApplicationDB();
        _application = _applicationdb.GetApplicationByID(_aid);

        _application.ApplicationStatus = ddlApplicationStatus.SelectedValue;
        _application.Remark = txtRemark.Text;
        _application.VerifiedBy = Int64.Parse(Session[CE.Global.General.HRId].ToString());

        Int64 _result = _applicationdb.InsertUpdate(_application);
        if (_result > 0)
        {
            Response.Redirect("job-application-list.aspx");
        }
    }
    protected void btnDownloadCV_Click(object sender, EventArgs e)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(hidCV.Value);
        if (!file.Exists)
        {
            Response.StatusCode = 500;
            Response.End();
            return;
        }
        Response.Clear();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        ApplicationInstance.CompleteRequest();
        Response.End();
    }
    protected void btnDownloadVR_Click(object sender, EventArgs e)
    {
        System.IO.FileInfo file = new System.IO.FileInfo(hidVR.Value);
        if (!file.Exists)
        {
            Response.StatusCode = 500;
            Response.End();
            return;
        }
        Response.Clear();
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        Response.Cache.SetExpires(DateTime.MinValue);
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        ApplicationInstance.CompleteRequest();
        Response.End();
    }
}
